package com.vimi8.ebb.auth.utils;

import com.vimi8.ebb.auth.model.RegUser;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by think on 2016/7/24.
 */
public class CommonUtil {
    //时间字符串转化为时间类型
    public static Date parseDateStr(String dateStr){
        try {
            return  new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    //把地址代码 11 01 01 001 006 分解成‘省’、‘市’、‘区/县’、‘乡’、‘村’
    public static Map<String,String> parseGeoCodings(String geoCodingsStr){
        Map<String,String> map=new HashMap<String,String>();
       String prov= (geoCodingsStr.substring(0,2)+"0000000000");
        map.put("prov",prov);
        String city= (geoCodingsStr.substring(0,4)+"00000000");
        map.put("city",city);
        String county= (geoCodingsStr.substring(0,6)+"000000");
        map.put("county",county);
        String vilage= (geoCodingsStr.substring(0,9)+"000");
        map.put("vilage",vilage);
        map.put("burg",geoCodingsStr);
        return map;
    }
    //传一个id返回每个字段的值
    public static Map<String,String> parseGeoCodingsField(String geoCodingsStr){
        Map<String,String> map=new HashMap<String,String>();
        String prov= (geoCodingsStr.substring(0,2));
        map.put("prov",(prov));
        String city= (geoCodingsStr.substring(2,4));
        map.put("city",(city));
        String county= (geoCodingsStr.substring(4,6));
        map.put("county",(county));
        String vilage= (geoCodingsStr.substring(6,9));
        map.put("vilage",(vilage));
        String burg=(geoCodingsStr.substring(9,12));
        map.put("burg",(burg));
        return map;
    }
    //判断一个codeId的形状区划 减少与数据的交互
    public static int caculateRegionLever(String codeId){
        int reginLever=0;
        if(null==codeId||"".equals(codeId))
            return reginLever;
        if(codeId.length()>12)return reginLever;
        int burgLever=Integer.valueOf(codeId.substring(9,12));
        if(burgLever>0)return 5;
        int villageLever=Integer.valueOf(codeId.substring(6,9));
        if(villageLever>0)return 4;
        int countyLever=Integer.valueOf(codeId.substring(4,6));
        if(countyLever>0)return 3;
        int cityLever=Integer.valueOf(codeId.substring(2,4));
        if(cityLever>0)return 2;
        int provLever=Integer.valueOf(codeId.substring(0,2));
        if(provLever>0)return 1;
        return reginLever;
    }
    public static String getDecimalPoorRate(int poorPersonTotal,int personTotal){
        double poorRate=(poorPersonTotal*1.0/personTotal)*100;
        String pooRateStr= new DecimalFormat("#.00").format(poorRate)+"%";
        return pooRateStr;
    }
//    public static void main(String[] args){
//        DecimalFormat    df   = new DecimalFormat("######0.00");
//
//        double d1 = 3.23456;
//        double d2 = 0.0;
//        double d3 = 2.0;
//        df.format(d1);
//        double fireRate=1*1.0/3*100;
//System.out.print("fireRate="+    df.format(fireRate));
//    }
// 将 s 进行 BASE64 编码
public static String getBASE64(String s) {
    if (s == null) return null;
    return Base64Utils.encodeToString(s.getBytes());
}

    // 将 BASE64 编码的字符串 s 进行解码
    public static String getFromBASE64(String s) {
        if (s == null) return null;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] b = decoder.decodeBuffer(s);
            return new String(b);
        } catch (Exception e) {
            return null;
        }
    }
    public static boolean isMobileNO(String lanLine) {
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
        //  0\d{2}-\d{8}|0\d{2}-\d{7}|0\d{3}-\d{7}|0\d{3}-\d{8}
        Matcher m = p.matcher(lanLine);
        System.out.println(m.matches() + "---");
        return m.matches();
    }
    public static boolean isLandlineNO(String mobiles) {
        Pattern p = Pattern.compile("^0\\d{2}-\\d{8}|0\\d{2}-\\d{7}|0\\d{3}-\\d{7}|0\\d{3}-\\d{8}$");
        Pattern p1 = Pattern.compile("^0\\d{2}\\d{8}|0\\d{2}\\d{7}|0\\d{3}\\d{7}|0\\d{3}\\d{8}$");
        //  0\d{2}-\d{8}|0\d{2}-\d{7}|0\d{3}-\d{7}|0\d{3}-\d{8}
        Matcher m = p.matcher(mobiles);
        Matcher m1 = p1.matcher(mobiles);
      //  System.out.println(m.matches() + "---");
        return m.matches()||m1.matches();
    }

    public static boolean is400NO(String mobiles) {
        Pattern p = Pattern.compile("^400-\\d{7}$");
        Pattern p1 = Pattern.compile("^400\\d{7}$");
        //  0\d{2}-\d{8}|0\d{2}-\d{7}|0\d{3}-\d{7}|0\d{3}-\d{8}
        Matcher m = p.matcher(mobiles);
        Matcher m1 = p1.matcher(mobiles);
       // System.out.println(m.matches() + "---");
        return m.matches()||m1.matches();
    }
    public static String MD5(String sourceStr) throws Exception{
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new Exception(e.getMessage());
        }
        return result;
    }
// public static void main(String[] args){
//
//     String landLine="08563913003";
//     boolean t=isLandlineNO(landLine);
//     System.out.print("is landLine test "+t);
//     String is400NO="4001234567";
//     boolean isLandLineNo=is400NO(is400NO);
//     System.out.print("isLandLineNo="+isLandLineNo);
//        String scopes="user|shareholder|user-me|admin|senior-admin|super-admin";
//        if(!StringUtils.isEmpty(scopes)){
//            scopes=scopes.replace("user-me|","");
//        }
//        System.out.print("scopes="+scopes);
//      String str=  MD5("POSTopenapi.xg.qq.com/v2/push/single_device Param1=Value1Param2=Value2access_id=123timestamp=1386691200abcde");
//        System.out.print("result="+str);
//
//        String landLine="0856-3913003";
//        boolean t=isLandlineNO(landLine);
//        System.out.print("is landLine "+t);
//
//        RegUser regUser=new RegUser();
//       regUser.setPhoneNumber("400-1618826");
//        if (regUser.getPhoneNumber() != null && !CommonUtil.isMobileNO(regUser.getPhoneNumber())&&!CommonUtil.isLandlineNO(regUser.getPhoneNumber())&&!CommonUtil.is400NO(regUser.getPhoneNumber())) {
//            // logger.info("phoneNumber validate failed.");
//            //throw new Exception("phoneNumber validate failed. ");
//            System.out.print("valide failed ");
//        }else{
//            System.out.print("ok");
//        }
//
//RegUser regUser=new RegUser();
//     regUser.setPhoneNumber("  ");
//     if (!StringUtils.isEmpty(regUser.getPhoneNumber())&&!StringUtils.isEmpty(regUser.getPhoneNumber().trim())   && !CommonUtil.isMobileNO(regUser.getPhoneNumber())&&!CommonUtil.isLandlineNO(regUser.getPhoneNumber())&&!CommonUtil.is400NO(regUser.getPhoneNumber())) {
//        System.out.print("error");
//
//     }else{
//         System.out.print("ok");
//     }
//
//   }

}
